﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Sunny.UI;

namespace RclSystem
{
    public partial class Receipt : UIPage
    {
        DataTable dataTable = new DataTable("DataTable");
        public Receipt()
        {
            InitializeComponent();
            uiDataGridView1.AutoGenerateColumns = false;
        }

        private void ListQuery()
        {
            dataTable.Rows.Clear();
            uiDataGridView1.Rows.Clear();
            string extsql = "";
            if (txt_saleman.TextLength != 0)
            {
                extsql += " and salesman = '" + txt_saleman.Text.Trim() + "'";
            }
            if (txt_company.TextLength != 0)
            {
                extsql += " and companyname like '%" + txt_company.Text + "%'";
            }
            dataTable = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where receiptdate >= '" + dt_qstart.Value.ToString("yyyy-MM-dd") + "' and receiptdate <= '"
                + dt_qend.Value.ToString("yyyy-MM-dd") + "'" + extsql + " order by receiptdate desc");

            int index = 0;
            decimal receiptamount_total = 0;
            double interest_total = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                index = this.uiDataGridView1.Rows.Add();
                this.uiDataGridView1.Rows[index].Cells[0].Value = row["id"].ToString();
                this.uiDataGridView1.Rows[index].Cells[1].Value = row["salesman"].ToString();
                this.uiDataGridView1.Rows[index].Cells[2].Value = row["companyname"].ToString();
                this.uiDataGridView1.Rows[index].Cells[3].Value = ((DateTime)row["receiptdate"]).ToString("yyyy-MM-dd");
                this.uiDataGridView1.Rows[index].Cells[4].Value = row["receiptway"].ToString();
                this.uiDataGridView1.Rows[index].Cells[5].Value = row["receiptamount"].ToString();
                this.uiDataGridView1.Rows[index].Cells[6].Value = row["overduedays"].ToString();
                this.uiDataGridView1.Rows[index].Cells[7].Value = row["interestrate"].ToString();
                this.uiDataGridView1.Rows[index].Cells[8].Value = row["interest"].ToString();
                this.uiDataGridView1.Rows[index].Cells[0].Tag = row["parentid"].ToString();
                this.uiDataGridView1.Rows[index].Cells[9].Value = ((DateTime)row["create_time"]).ToString("yyyy-MM-dd HH:mm:ss");

                receiptamount_total += decimal.Parse(row["receiptamount"].ToString());
                interest_total += double.Parse(row["interest"].ToString());

                // 添加条件设置字体颜色
                if (row["parentid"].ToString() == "-1")
                {
                    this.uiDataGridView1.Rows[index].Cells[5].Style.BackColor = Color.CadetBlue;
                }
            }
            //生成合计行
            index = this.uiDataGridView1.Rows.Add();
            this.uiDataGridView1.Rows[index].Cells[0].Value = "";
            this.uiDataGridView1.Rows[index].Cells[1].Value = "合计";
            this.uiDataGridView1.Rows[index].Cells[2].Value = "";
            this.uiDataGridView1.Rows[index].Cells[3].Value = "";
            this.uiDataGridView1.Rows[index].Cells[4].Value = "";
            this.uiDataGridView1.Rows[index].Cells[5].Value = Math.Round(receiptamount_total,2);
            this.uiDataGridView1.Rows[index].Cells[6].Value = "";
            this.uiDataGridView1.Rows[index].Cells[7].Value = "";
            this.uiDataGridView1.Rows[index].Cells[8].Value = Math.Round(interest_total, 2);
            this.uiDataGridView1.Rows[index].Cells[9].Value = "";
            uiDataGridView1.ClearSelection();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            decimal ramount = 0;//多余的结算金额
            ReceiptDialog frm = new ReceiptDialog();
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                Dictionary<string, object> data = new Dictionary<string, object>();
                data.Add("salesman", frm.ReceiptEntity.salesman);
                data.Add("companyname", frm.ReceiptEntity.companyname);
                data.Add("receiptdate", frm.ReceiptEntity.receiptdate);
                data.Add("receiptway", frm.ReceiptEntity.receiptway);
                data.Add("receiptamount", frm.ReceiptEntity.receiptamount);
                data.Add("overduedays", frm.ReceiptEntity.overduedays);
                data.Add("interestrate", frm.ReceiptEntity.interestrate);
                data.Add("interest", frm.ReceiptEntity.interest);
                data.Add("create_time", DateTime.Now);
                data.Add("parentid", frm.ReceiptEntity.parentid);
                using (MySqlConnection connection = new MySqlConnection(Program.connectionString))
                {
                    // 打开连接
                    connection.Open();
                    MySqlTransaction transaction = connection.BeginTransaction();
                    if(data["parentid"].ToString() == "0")
                    {
                        //偿还期初数的收款单
                        using (MySqlCommand command = new MySqlCommand("SELECT * FROM tbl_company where name = '" + frm.ReceiptEntity.companyname + "'", connection))
                        {
                            command.Transaction = transaction;
                            // 创建DataAdapter和DataTable对象，并填充数据
                            using (MySqlDataAdapter adapter = new MySqlDataAdapter(command))
                            {
                                try
                                {
                                    DataTable dt = new DataTable();
                                    adapter.Fill(dt);
                                    if (dt.Rows.Count > 0)
                                    {
                                        DataRow row = dt.Rows[0];
                                        decimal initalamount = 0;
                                        if (decimal.TryParse(row["initalamount"].ToString(), out initalamount))
                                        {
                                            if(frm.ReceiptEntity.receiptamount > initalamount)
                                            {
                                                this.ShowErrorDialog2("收款金额超出期初数金额,请拆单结算");
                                                transaction.Rollback();
                                                return;
                                            }
                                            ramount = initalamount - frm.ReceiptEntity.receiptamount;

                                            string sqlstr = "INSERT INTO tbl_receipt (" +
                                                "salesman,companyname,receiptdate,receiptway,receiptamount,overduedays,interestrate,interest,create_time,parentid" +
                                            ") VALUES('" + data["salesman"].ToString() + "','" + data["companyname"].ToString() + "','" + data["receiptdate"].ToString() + "','" + data["receiptway"].ToString() + "'," +
                                                frm.ReceiptEntity.receiptamount + "," + data["overduedays"].ToString() + "," + data["interestrate"].ToString() + "," +
                                                data["interest"].ToString() + ",'" + DateTime.Now + "'," + 0 + ");";

                                            using (MySqlCommand cmd = new MySqlCommand(sqlstr, connection))
                                            {
                                                cmd.Transaction = transaction;
                                                cmd.ExecuteNonQuery();
                                            }

                                            //更新单位期初数
                                            using (MySqlCommand cmd = new MySqlCommand("update tbl_company set initalamount = " + ramount + " where id = " + row["id"].ToString(), connection))
                                            {
                                                cmd.Transaction = transaction;
                                                cmd.ExecuteNonQuery();
                                            }
                                        }
                                        else
                                        {
                                            this.ShowErrorDialog2("期初数金额:" + row["initalamount"].ToString() + "异常");
                                            transaction.Rollback();
                                            return;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    this.ShowErrorDialog2(ex.Message);
                                    transaction.Rollback();
                                    return;
                                }
                            }
                            transaction.Commit();
                        }
                    }
                    else
                    {
                        try
                        {
                            //偿还送货单
                            DataTable dt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where id = " + data["parentid"].ToString());
                            //开具收款单
                            string sqlstr = "INSERT INTO tbl_receipt (" +
                                                    "salesman,companyname,receiptdate,receiptway,receiptamount,overduedays,interestrate,interest,create_time,parentid" +
                                                ") VALUES('" + data["salesman"].ToString() + "','" + data["companyname"].ToString() + "','" + data["receiptdate"].ToString() + "','" + data["receiptway"].ToString() + "'," +
                                                    frm.ReceiptEntity.receiptamount + "," + data["overduedays"].ToString() + "," + data["interestrate"].ToString() + "," +
                                                    data["interest"].ToString() + ",'" + DateTime.Now + "'," + data["parentid"].ToString() + ");";

                            using (MySqlCommand cmd = new MySqlCommand(sqlstr, connection))
                            {
                                cmd.Transaction = transaction;
                                cmd.ExecuteNonQuery();
                            }
                            //更新送货单剩余金额                        
                            decimal remainamount = 0;
                            if (decimal.TryParse(dt.Rows[0]["remainamount"].ToString(), out remainamount))
                            {
                                ramount = remainamount - frm.ReceiptEntity.receiptamount;
                                if (ramount < 0)
                                {
                                    this.ShowErrorDialog2("收款金额超出发货单剩余结算金额,请拆单结算");
                                    transaction.Rollback();
                                    return;
                                }
                                else
                                {
                                    //更新发货单剩余结算金额
                                    using (MySqlCommand cmd = new MySqlCommand("update tbl_delivery set remainamount = " + ramount + " where id = " + data["parentid"].ToString(), connection))
                                    {
                                        cmd.Transaction = transaction;
                                        cmd.ExecuteNonQuery();
                                    }

                                }
                            }
                            else
                            {
                                this.ShowErrorDialog2("送货单剩余结算金额:" + dt.Rows[0]["remainamount"].ToString() + "异常");
                                transaction.Rollback();
                                return;
                            }
                        }
                        catch
                        {
                            transaction.Rollback();
                            return;
                        }
                        
                        transaction.Commit();
                    }
                    /*using (MySqlCommand command = new MySqlCommand("SELECT * FROM tbl_company where name = '" + frm.ReceiptEntity.companyname + "'", connection))
                    {
                        command.Transaction = transaction;
                        // 创建DataAdapter和DataTable对象，并填充数据
                        using (MySqlDataAdapter adapter = new MySqlDataAdapter(command))
                        {
                            try
                            {
                                DataTable dt = new DataTable();
                                adapter.Fill(dt);
                                if (dt.Rows.Count > 0)
                                {
                                    DataRow row = dt.Rows[0];
                                    decimal initalamount = 0;
                                    if (decimal.TryParse(row["initalamount"].ToString(), out initalamount))
                                    {
                                        initalamount = Math.Round(initalamount,2);
                                        ramount = frm.ReceiptEntity.receiptamount + initalamount;
                                        //判断单位是否有期初数
                                        if (initalamount < 0)
                                        {
                                            //如果期初数是负数，先偿还期初数
                                            //自动开收款是负数的收款单
                                            string sqlstr = "INSERT INTO tbl_receipt (" +
                                                "salesman,companyname,receiptdate,receiptway,receiptamount,overduedays,interestrate,interest,create_time,parentid" +
                                            ") VALUES('" + data["salesman"].ToString() + "','" + data["companyname"].ToString() + "','" + data["receiptdate"].ToString() + "','" + data["receiptway"].ToString() + "'," +
                                                initalamount + "," + data["overduedays"].ToString() + "," + data["interestrate"].ToString() + "," +
                                                data["interest"].ToString() + ",'" + DateTime.Now + "'," + 0 + ");";

                                            using (MySqlCommand cmd = new MySqlCommand(sqlstr, connection))
                                            {
                                                cmd.Transaction = transaction;
                                                cmd.ExecuteNonQuery();
                                            }
                                            //更新单位期初数
                                            if (ramount >= 0)
                                            {
                                                using (MySqlCommand cmd = new MySqlCommand("update tbl_company set initalamount = 0 where id = " + row["id"].ToString(), connection))
                                                {
                                                    cmd.Transaction = transaction;
                                                    cmd.ExecuteNonQuery();
                                                }
                                                initalamount = 0;
                                                data["receiptamount"] = ramount;
                                            }
                                            else
                                            {
                                                //收款金额小于期初数，不足以偿还
                                                using (MySqlCommand cmd = new MySqlCommand("update tbl_company set initalamount = " + ramount + " where id = " + row["id"].ToString(), connection))
                                                {
                                                    cmd.Transaction = transaction;
                                                    cmd.ExecuteNonQuery();
                                                }
                                                transaction.Commit();
                                                return;
                                            }
                                            
                                        }
                                        if (initalamount == 0)
                                        {                                   

                                            //有多余的收款结账金额,获取发货单结算
                                            dt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where companyname = '" + frm.ReceiptEntity.companyname + "' and remainamount > 0 order by duedate limit 1");
                                            if (dt.Rows.Count > 0)
                                            {
                                                row = dt.Rows[0];
                                                decimal remainamount = 0;
                                                if (decimal.TryParse(row["remainamount"].ToString(), out remainamount))
                                                {
                                                    if (ramount > remainamount)
                                                    {
                                                        this.ShowErrorDialog2("收款金额超出发货单剩余结算金额,请拆单结算");
                                                        transaction.Rollback();
                                                        return;
                                                    }
                                                    else
                                                    {
                                                        //对应发货单据
                                                        data.Add("parentid", long.Parse(row["id"].ToString()));
                                                        //更新发货单剩余结算金额
                                                        using (MySqlCommand cmd = new MySqlCommand("update tbl_delivery set remainamount = " + (remainamount - ramount) + " where id = " + row["id"].ToString(), connection))
                                                        {
                                                            cmd.Transaction = transaction;
                                                            cmd.ExecuteNonQuery();
                                                        }
                                                        ramount = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    this.ShowErrorDialog2("异常的剩余结款数据:" + row["remainamount"].ToString());
                                                    transaction.Rollback();
                                                    return;
                                                }
                                            }
                                            else
                                            {
                                                this.ShowErrorDialog2("未找到发货单对应收款单.");
                                                transaction.Rollback();
                                                return;
                                            }
                                        }
                                        else if (frm.ReceiptEntity.receiptamount > initalamount )
                                        {
                                            this.ShowErrorDialog2("收款金额超出期初数金额,请拆单结算");
                                            transaction.Rollback();
                                            return;
                                        }
                                        else
                                        {
                                            ramount = initalamount - frm.ReceiptEntity.receiptamount;
                                            initalamount = 0;
                                            //对应单据无
                                            data.Add("parentid", 0);
                                            //更新单位期初数
                                            using (MySqlCommand cmd = new MySqlCommand("update tbl_company set initalamount = " + ramount + " where id = " + row["id"].ToString(), connection))
                                            {
                                                cmd.Transaction = transaction;
                                                cmd.ExecuteNonQuery();
                                            }
                                        }
                                    }

                                }
                                int result = MySQLHelper.Insert("tbl_receipt", data);
                                ListQuery();
                            }catch(Exception ex)
                            {
                                transaction.Rollback();
                                return;
                            }
                            transaction.Commit();
                        }
                    }*/
                    
                }
                    //DataTable dt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where name = '" + frm.ReceiptEntity.companyname);
                
                
                
            }

            frm.Dispose();
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_del" && e.RowIndex >= 0)
            {
                if (uiDataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() == "合计")
                {
                    return;
                }
                if (this.ShowAskDialog2("确定要删除该数据么?", true))
                {
                    //获取详细数据
                    DataGridViewRow vr = uiDataGridView1.Rows[e.RowIndex];
                    DataTable dt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where id = " + vr.Cells[0].Value.ToString());
                    if(dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        //开始删除事务
                        using (MySqlConnection connection = new MySqlConnection(Program.connectionString))
                        {
                            // 打开连接
                            connection.Open();
                            MySqlTransaction transaction = connection.BeginTransaction();
                            long parentid = 0;
                            if(!long.TryParse(row["parentid"].ToString(), out parentid))
                            {
                                this.ShowErrorDialog2("对应出货单异常,删除失败.");
                                transaction.Rollback();
                                return;
                            }
                            try
                            {
                                if (parentid > 0)
                                {
                                    //获取对应出货单
                                    DataTable dt_delivery = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where id = " + parentid);
                                    if (dt_delivery.Rows.Count > 0)
                                    {
                                        DataRow deliver = dt_delivery.Rows[0];
                                        //更新出货单剩余结算金额
                                        using (MySqlCommand cmd = new MySqlCommand("update tbl_delivery set remainamount = remainamount + " + double.Parse(row["receiptamount"].ToString()) + " where id = " + deliver["id"].ToString(), connection))
                                        {
                                            cmd.Transaction = transaction;
                                            cmd.ExecuteNonQuery();
                                        }
                                    }
                                    else
                                    {
                                        this.ShowErrorDialog2("对应出货单异常,删除失败.");
                                        transaction.Rollback();
                                        return;
                                    }
                                }
                                else
                                {
                                    //获取对应单位期初数
                                    DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where name = '" + row["companyname"].ToString() + "'");
                                    if (dt_company.Rows.Count > 0)
                                    {
                                        DataRow company = dt_company.Rows[0];
                                        //更新期初数
                                        using (MySqlCommand cmd = new MySqlCommand("update tbl_company set initalamount = initalamount + " + double.Parse(row["receiptamount"].ToString()) + " where id = " + company["id"].ToString(), connection))
                                        {
                                            cmd.Transaction = transaction;
                                            cmd.ExecuteNonQuery();
                                        }
                                    }
                                    else
                                    {
                                        this.ShowErrorDialog2("对应单位期初数异常,删除失败.");
                                        transaction.Rollback();
                                        return;
                                    }
                                }
                                //删除收款单
                                using (MySqlCommand cmd = new MySqlCommand("delete from tbl_receipt where id = " + vr.Cells[0].Value.ToString(), connection))
                                {
                                    cmd.Transaction = transaction;
                                    cmd.ExecuteNonQuery();
                                }
                                //int result = MySQLHelper.Delete("tbl_receipt", "id=" + vr.Cells[0].Value.ToString());
                                transaction.Commit();
                                ListQuery();
                            }
                            catch(Exception ex)
                            {
                                transaction.Rollback();
                                return;
                            }
                            
                        }
                            
                    }
                    else
                    {
                        this.ShowErrorDialog2("未找到对应的收款单.");
                    }
                    
                }

            }
            else if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_edit" && e.RowIndex >= 0)
            {
                if (uiDataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() == "合计")
                {
                    return;
                }
                DataGridViewRow vr = uiDataGridView1.Rows[e.RowIndex];
                ReceiptEntity receipt = new ReceiptEntity();
                receipt.id = long.Parse(vr.Cells[0].Value.ToString());
                receipt.salesman = vr.Cells[1].Value.ToString();
                receipt.companyname = vr.Cells[2].Value.ToString();
                receipt.receiptdate = DateTime.Parse(vr.Cells[3].Value.ToString());
                receipt.receiptway = vr.Cells[4].Value.ToString();
                receipt.receiptamount = decimal.Parse(vr.Cells[5].Value.ToString());
                receipt.overduedays = int.Parse(vr.Cells[6].Value.ToString());
                receipt.interestrate = double.Parse(vr.Cells[7].Value.ToString());
                receipt.interest = double.Parse(vr.Cells[8].Value.ToString());
                receipt.parentid = long.Parse(vr.Cells[0].Tag.ToString());
                ReceiptDialog frm = new ReceiptDialog();
                frm.Render();
                decimal oamount = receipt.receiptamount; //记录原始收款金额
                frm.ReceiptEntity = receipt;
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    //获取详细数据
                    DataTable dt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where id = " + vr.Cells[0].Value.ToString());
                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        using (MySqlConnection connection = new MySqlConnection(Program.connectionString))
                        {
                            // 打开连接
                            connection.Open();
                            MySqlTransaction transaction = connection.BeginTransaction();
                            try
                            {
                                //判断收款金额是否变动
                                decimal diffvalue = frm.ReceiptEntity.receiptamount - oamount;
                                if (diffvalue != 0)
                                {
                                    //需要更新出货单剩余结算金额
                                    //获取对应出货单
                                    long parentid = 0;
                                    if (!long.TryParse(row["parentid"].ToString(), out parentid))
                                    {
                                        this.ShowErrorDialog2("对应出货单异常,删除失败.");
                                        transaction.Rollback();
                                        return;
                                    }
                                    if (parentid > 0)
                                    {
                                        DataTable dt_delivery = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where id = " + parentid);
                                        if (dt_delivery.Rows.Count > 0)
                                        {
                                            DataRow deliver = dt_delivery.Rows[0];
                                            //更新出货单剩余结算金额
                                            using (MySqlCommand cmd = new MySqlCommand("update tbl_delivery set remainamount = remainamount - " + diffvalue + " where id = " + deliver["id"].ToString(), connection))
                                            {
                                                cmd.Transaction = transaction;
                                                cmd.ExecuteNonQuery();
                                            }
                                        }
                                        else
                                        {
                                            this.ShowErrorDialog2("对应出货单异常,删除失败.");
                                            transaction.Rollback();
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        //获取对应单位期初数
                                        DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where name = " + row["companyname"].ToString());
                                        if (dt_company.Rows.Count > 0)
                                        {
                                            DataRow company = dt_company.Rows[0];
                                            //更新期初数
                                            using (MySqlCommand cmd = new MySqlCommand("update tbl_company set initalamount = initalamount - " + diffvalue + " where id = " + company["id"].ToString(), connection))
                                            {
                                                cmd.Transaction = transaction;
                                                cmd.ExecuteNonQuery();
                                            }
                                        }
                                        else
                                        {
                                            this.ShowErrorDialog2("对应单位期初数异常,删除失败.");
                                            transaction.Rollback();
                                            return;
                                        }
                                    }
                                }
                                //更新收款单
                                Dictionary<string, object> data = new Dictionary<string, object>();
                                data.Add("salesman", frm.ReceiptEntity.salesman);
                                data.Add("companyname", frm.ReceiptEntity.companyname);
                                data.Add("receiptdate", frm.ReceiptEntity.receiptdate);
                                data.Add("receiptway", frm.ReceiptEntity.receiptway);
                                data.Add("receiptamount", frm.ReceiptEntity.receiptamount);
                                data.Add("overduedays", frm.ReceiptEntity.overduedays);
                                data.Add("interestrate", frm.ReceiptEntity.interestrate);
                                data.Add("interest", frm.ReceiptEntity.interest);
                                int result = MySQLHelper.Update("tbl_receipt", data, "id=" + frm.ReceiptEntity.id);
                                if(result == 0)
                                {
                                    transaction.Rollback();
                                    return;
                                }
                                transaction.Commit();
                                ListQuery();
                            }
                            catch(Exception ex)
                            {
                                this.ShowErrorDialog2(ex.Message);
                                transaction.Rollback();
                                return;
                            }
                            
                        }
                    }
                }

                frm.Dispose();
            }
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            ListQuery();
        }

        private void Receipt_Initialize(object sender, EventArgs e)
        {
            dt_qend.Value = DateTime.Now;
            dt_qstart.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            ListQuery();
        }

        private void uiDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                // 检查是否是按钮列
                if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_del" || uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_edit")
                {
                    if ((e.ColumnIndex == 10 || e.ColumnIndex == 11) && uiDataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() == "合计")
                    {
                        // 跳过绘制按钮
                        e.Handled = true;
                    }
                }
            }
        }

        private void btn_addpre_Click(object sender, EventArgs e)
        {
            PreReceiptDialog frm = new PreReceiptDialog();
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                Dictionary<string, object> data = new Dictionary<string, object>();
                data.Add("salesman", frm.ReceiptEntity.salesman);
                data.Add("companyname", frm.ReceiptEntity.companyname);
                data.Add("receiptdate", frm.ReceiptEntity.receiptdate);
                data.Add("receiptway", frm.ReceiptEntity.receiptway);
                data.Add("receiptamount", frm.ReceiptEntity.receiptamount);
                data.Add("overduedays", frm.ReceiptEntity.overduedays);
                data.Add("interestrate", frm.ReceiptEntity.interestrate);
                data.Add("interest", frm.ReceiptEntity.interest);
                data.Add("create_time", DateTime.Now);
                data.Add("parentid", -1);
                using (MySqlConnection connection = new MySqlConnection(Program.connectionString))
                {
                    // 打开连接
                    connection.Open();
                    int result = MySQLHelper.Insert("tbl_receipt", data);
                    ListQuery();
                }
               
            }

            frm.Dispose();
        }
    }
}
